
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShow: false,
        userBalance: 0, //账户余额
        list: [],
        showRuleBox: false,
        showBox: false,
        sendUserId: '',
        sendPoints: '',
        checkFlag: 0,// 0 未校验  1 校验通过  2 校验失败
        checkUserInfo: ''
    },


    checkHandle() {
        if (this.data.sendUserId == '') {
            wx.showToast({
                title: '请输入ID',
                icon: 'none'
            })
            return
        }

        var that = this, url = app.globalData.url + 'user/selectUser', checkFlag = 0, checkUserInfo = '';

        wx.showLoading({
            title: '',
        })
        app.wxRequest(url, {
            id: that.data.sendUserId,
            flag: 0,
            name: 0,
        }, res => {
            console.log(res);
            if (res.flag) {

                if (res.data.length != 0) {
                    checkFlag = 1
                    checkUserInfo = res.data[0]
                } else {
                    checkFlag = 2
                }

                wx.hideLoading()
                that.setData({
                    checkFlag, checkUserInfo
                })
            }
        },
            err => {
                console.log(err);
            })



    },
    // 转赠
    zengHandle() {
        if (this.data.userBalance > 0) {
            this.setData({
                showBox: true
            })
        } else {
            wx.showToast({
                title: '您的账户暂无可转赠积分',
                icon: 'none'
            })
        }

    },
    zengBoxHandle(e) {
        var flag = e.currentTarget.dataset.flag;
        if (flag == 0) {
            this.setData({
                showBox: false,
                sendUserId: '',
                sendPoints: '',
                checkFlag: 0
            })
        } else {
            var checkFlag = this.data.checkFlag, sendUserId = this.data.sendUserId, sendPoints = this.data.sendPoints, that = this, url = app.globalData.url + 'points/increasePoints';
            if (sendUserId == '') {
                wx.showToast({
                    title: '请输入用户id',
                    icon: 'none'
                })
                return
            }
            if (checkFlag == '0') {
                wx.showToast({
                    title: '请校验用户ID',
                    icon: 'none'
                })
                return
            }
            if (checkFlag == '2') {
                wx.showToast({
                    title: '请仔细核对用户ID',
                    icon: 'none'
                })
                return
            }
            if (sendPoints > this.data.userBalance) {
                wx.showToast({
                    title: '不可超过账户积分',
                    icon: 'none'
                })
                return
            }
            if (sendPoints == 0) {
                wx.showToast({
                    title: '最少转赠1个积分',
                    icon: 'none'
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '仔细核对用户ID，此操作不可撤回，请谨慎操作！',
                cancelText: '再想想',
                complete: (res) => {
                    if (res.cancel) {

                    }
                    if (res.confirm) {
                        console.log(sendUserId);
                        console.log(sendPoints);

                        wx.showLoading({
                            title: '',
                        })
                        app.wxRequest(url, {
                            senderId: app.globalData.userId,
                            receiverId: sendUserId,
                            point: sendPoints
                        }, res => {
                            console.log(res);
                            if (res.flag) {
                                this.setData({
                                    showBox: false,
                                    sendUserId: '',
                                    sendPoints: '',
                                    checkFlag: 0
                                })
                                wx.hideLoading()
                                wx.showToast({
                                    title: '转赠成功',
                                    icon: 'none'
                                })
                                that.getList()
                            }
                        },
                            err => {
                                console.log(err);
                            })
                    }
                }
            })


        }

    },
    // 输入框编辑
    inputHandle(e) {
        var str = e.currentTarget.dataset.flag, val = e.detail.value;
        if (str == 'sendPoints') {
            if (val > this.data.userBalance) {
                wx.showToast({
                    title: '不可超过账户积分',
                    icon: 'none'
                })
                val = 1
            }
        }

        this.setData({
            [str]: val
        })
        console.log(this.data);
    },

    getList() {

        var url = app.globalData.url + "points/listPoints", that = this, list = [];
        wx.showLoading({
            title: '',
        })
        app.wxRequest(url, {
            userId: app.globalData.userId
        }, res => {
            console.log(res);
            if (res.flag) {
                var datas = res.data;
                console.log(datas);
                wx.hideLoading()
                wx.stopPullDownRefresh()
                datas.notExpired.reverse()
                datas.transferOut.reverse()
                datas.isExpired.reverse()

                datas.notExpired.forEach(item => {
                    item.isPass = false
                    list.push(item)
                })

                datas.transferOut.forEach(item => {
                    item.isPass = false
                    list.push(item)
                })

                datas.isExpired.forEach(item => {
                    item.isPass = true
                    list.push(item)
                })

                list = list.sort((a, b) => {
                    return b.pointId - a.pointId
                })

                that.setData({
                    userBalance: datas.remainingTotalScore,
                    list
                })
            }
        },
            err => {
                console.log(err);
            })

    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getList()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})